<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
        //获取左侧分类树
        $temp=array();
        $temp=$this->getListCat();
        $type_brand=M('type_brand a');
        $Model = new \Think\Model();
        foreach($temp as $key=>&$value)
        {
            if($value['level']===1)
            {
             $brands=$Model->query('SELECT a.brandid ,b.brandname,a.typeid FROM cart_type_brand a  inner JOIN cart_brand b on a.brandid=b.brandid where a.typeid='.$value['typeid']);

                if(!empty($brands))
                {
                    $value['brand']=$brands;
                }else{
                    $value['brand']=array();
                }
            }
            unset($value['level']);

        }

        $pids=array();
        foreach($temp as $k=>$v){
            if($v['pid']==0){
                $pids[]=$k;
            }
        }
        $ret=array();
        foreach($pids as $key=>&$value){
            foreach($temp as $k=>$v){
                if($value==$v['pid']){
                                $ret[$value]['typeid']=$value;
                    $ret[$value]['leve2'][]=$v;

                }elseif ($value==$v['typeid']) {
                    $ret[$value]['typename']=$v['typename'];
                    $ret[$value]['imgpath']=$v['imgpath'];
                }
            }
                                unset($value);
        }

        $result=array();
        foreach($ret as $key=>$value){
            $result[]=$value;
        }
        $res=array();
        if(!empty($result)){
            $res['status']=1;
            $res['tree']=$result;
        }else
        {
            $res['status']=0;
        }
        //改变品牌的id为：typeid+brandid
        echo json_encode($res,JSON_UNESCAPED_UNICODE);
    }
    //获取首页底部二级分类
   /* public function secCate()
    {
        header("Content-Type: text/html; charset=UTF-8");
        $result=array();
        $temp=M('type')->getField('typeid,typename,pid,imgpath');
        foreach( $temp as $key=>&$value){
            if($value['pid']==0){
                unset($temp[$key]);
            }else{

                $result[]=$temp[$key];
            }
        }
        $res=array();
        if(!empty($result)){
            $res['status']=1;
            $res['tree']=$result;
        }else{
            $res['status']=0;
        }
        echo json_encode($res, JSON_UNESCAPED_UNICODE);

    }*/
     public function secCate()
    {
        header("Content-Type: text/html; charset=UTF-8");
        $areatype=I('get.areatype','2','strip_tags');
      $devicecode=I('get.devicecode','','strip_tags');
    //通过设备寻找运营商ID
    $yid= Operator($devicecode);
    if($areatype==2)
    {
        $where['areatype']=$areatype;
    }else
    {
         $where['areatype']=$areatype;
         $where['yid']=$yid;

    }
    $typeids=M('item')->where($where)->getField("typeid",true);
    if(empty($typeids))
    {
        $typeids[]=0;
    }
    $typeids=implode(",", $typeids);
    $where['typeid']=array("in",$typeids);
        $result=array();

        $temp=M('type')->where($where)->getField('typeid,typename,pid,imgpath');
       
        foreach( $temp as $key=>&$value){
            if($value['pid']==0){
                unset($temp[$key]);
            }else{

                $result[]=$temp[$key];
            }
        }
        $res=array();
        if(!empty($result)){
            $res['status']=1;
            $res['tree']=$result;
        }else{
            $res['status']=0;
            $res['msg']='暂无信息';
        }
        echo json_encode($res, JSON_UNESCAPED_UNICODE);

    }
    //获取背景图
    public function getImg(){

    }

        /**
     *
     * 返回已经确定子分类的类别属性，计算等级
     *
     */
    public function getListCat($data = array(),$level = 0) {
        $cats = array();
        !$data && $data = $this->getCats();
        if(!$data) return $cats;
        foreach($data as $key => $val) {
            $cats[$val['typeid']] = &$data[$key];
            $cats[$val['typeid']]['level'] = $level;
            if( isset( $val['children'] ) ) {

                $cats += $this->getListCat($val['children'],$level + 1);
                unset($data[$key]['children']);
            }
        }
        return $cats;
    }

    /**
     *
     * 获取所有类别
     *
     */
    protected function getCats($return = 'tree') {
        static $retarr;
        if(isset($retarr[$return])) return $retarr[$return];

        $cats = M('type')->order("sort")->getField('typeid,typename,pid,imgpath');
        if($return == 'tree')
        {           //返回数组
            $tree = array();

            foreach($cats as $cat)
             {  //循环cat
                if(isset($cats[$cat['pid']]))
                {//非第一级
                    $cats[$cat['pid']]['children'][] = &$cats[$cat['typeid']];
                } else
                 {//第一级
                    $tree[] = &$cats[$cat['typeid']];

                }
            }
            $retarr[$return] = $tree;
        } else if($return == 'source')
         {//返回source
            $retarr[$return] = $cats;
        }
        return $retarr[$return];
    }


}